package cn.bdqn.applysystem.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.bdqn.applysystem.entity.Grade;
import cn.bdqn.applysystem.entity.Subject;
import cn.bdqn.applysystem.entity.User;
import cn.bdqn.applysystem.service.GradeService;
import cn.bdqn.applysystem.service.SubjectService;
import cn.bdqn.applysystem.service.UserService;

@Controller("loginController")
public class LoginController {
	
	@Autowired
	@Qualifier("subjectServiceImpl")
	private SubjectService subjectService;
	
	@Autowired
	@Qualifier("userServiceImpl")
	private UserService userService;
	
	@Autowired
	@Qualifier("gradeServiceImpl")
	private GradeService gradeService;
	
	@RequestMapping(value="/login.html",method=RequestMethod.POST)
	public String doLogin(HttpServletRequest request,HttpSession session,String username,String password){
		String result = null;
		User user = userService.login(username, password);
		if(null != user){
			if(user.getRole().getId().equals(4)){
				List<Subject> subjectList = subjectService.getSubjectList();
				session.setAttribute("subjectList",subjectList);
				session.setAttribute("user", user);
				result = "redirect:/user/main.html";
			}else if(user.getRole().getId().equals(2)){
				List<Grade> gradeList = gradeService.findGradeListByHeadTeacherId(user.getId());
				session.setAttribute("gradeList",gradeList);
				session.setAttribute("user", user);
				result = "formteacher/index";
			}else{
				result = "login";
			}
		}else{
			request.setAttribute("message", "用户名或密码不正确");
			result = "login";
		}
		return result;
	}
	@RequestMapping(value="/login.html",method=RequestMethod.GET)
	public String doOutLogin(HttpSession session){
		session.invalidate();
		return "login";
	}
}
